
package com.yimixian.app.goods;

import com.yimixian.app.R;
import com.yimixian.app.util.Strings;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class SectionItemView extends FrameLayout {

    @InjectView(R.id.title_text)
    TextView mTitleView;
    @InjectView(R.id.subtitle_text)
    TextView mSubtitleView;

    private int mSectionId;

    public SectionItemView(Context context) {
        super(context);
        initSectionItemView();
    }

    public SectionItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSectionItemView();
    }

    public SectionItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initSectionItemView();
    }

    private void initSectionItemView() {
        LayoutInflater.from(getContext()).inflate(R.layout.section_item_view, this);
        ButterKnife.inject(this);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                getResources().getDimensionPixelSize(R.dimen.section_item_height));
        setLayoutParams(params);
    }

    public void init(String title, String subTitle, int sectionId) {
        mTitleView.setText(title);
        mSubtitleView.setText(subTitle);
        mSubtitleView.setVisibility(Strings.isNullOrEmpty(subTitle) ? GONE : VISIBLE);
        mSectionId = sectionId;
    }

    public int getSectionId() {
        return mSectionId;
    }

    public void setClicked(boolean clicked) {
        mTitleView.setTextColor(clicked ? getResources().getColor(R.color.ymx_orange) : getResources().getColor(R.color.category_title_normal));
        mSubtitleView.setTextColor(clicked ? getResources().getColor(R.color.category_subtitle_normal) : getResources().getColor(R.color.category_subtitle_normal));
        setBackgroundResource(clicked ? R.drawable.section_item_clicked_bg
                : R.drawable.section_item_normal_bg);
    }

    public String getTextViewTitle() {
        if(mTitleView != null) {
            return mTitleView.getText().toString().trim();
        } else {
            return null;
        }

    }
}
